class Admin::BaiVietController < ApplicationController
  include ApplicationHelper
  def index
    @trang = (params[:page].present? && params[:page].to_i > 1 ?  params[:page] : 1).to_i
    @danh_muc = DanhMuc.find_by_slug(params[:danh_muc])
    @danh_muc_ngon_ngu = @danh_muc.danh_muc_ngon_ngus.select{ |v| v.ngon_ngu == I18n.locale.to_s }
    if (@danh_muc_ngon_ngu.empty?)
      @danh_muc_ngon_ngu = @danh_muc.danh_muc_ngon_ngus[0]
    else
      @danh_muc_ngon_ngu= @danh_muc_ngon_ngu[0]
    end

    if ['da_xoa'].include?(params[:loc])
      query = BaiViet.where(:danh_muc_id => @danh_muc.id, :dang_kich_hoat => false)
    else
      query = BaiViet.where(:danh_muc_id => @danh_muc.id, :dang_kich_hoat => true)
    end
    
    @bai_viets = query.paginate(
      :order => :created_at.asc,
      :per_page => 20,
      :page => @trang
    )
    @co_trang_sau = !query.paginate(
      :order => :ten_hien_thi.asc,
      :per_page => 20,
      :page => @trang + 1
    ).empty?
    @co_trang_truoc = @trang > 1
    @index_dau_tien = (@trang - 1) * 20
  end

  def cap_nhat
    @bai_viet = BaiViet.find(params[:id])
    @danh_muc = DanhMuc.find_by_slug(params[:danh_muc])
    @danh_muc_ngon_ngu = @danh_muc.danh_muc_ngon_ngus.select{ |v| v.ngon_ngu == I18n.locale.to_s }
    if (@danh_muc_ngon_ngu.empty?)
      @danh_muc_ngon_ngu = @danh_muc.danh_muc_ngon_ngus[0]
    else
      @danh_muc_ngon_ngu= @danh_muc_ngon_ngu[0]
    end
    @nguoi_dung = nguoi_dung_hien_tai
    if (params[:commit])
      # bai_viet = BaiViet.create({
        # :tieu_de => params[:tieu_de],
        # :noi_dung => params[:noi_dung],
        # :danh_muc => @danh_muc,
        # :nguoi_dung => @nguoi_dung,
        # :ngon_ngu => params[:ngon_ngu]
      # })
      @bai_viet.tieu_de = params[:tieu_de].strip
      @bai_viet.noi_dung = params[:noi_dung].strip
      @bai_viet.ngon_ngu = params[:ngon_ngu].strip
      if @bai_viet.save
        flash.now[:thanh_cong] = t('cap_nhat_thanh_cong')
        else
        flash.now[:loi] = t('kiem_tra_lai_thong_tin')
      end
    end
  end

  def xoa
    nguoi_dung = nguoi_dung_hien_tai()
    if nguoi_dung.present? && nguoi_dung.is_admin && params[:uid].present?
      bai_viet = BaiViet.find(params[:uid])
      if (bai_viet.present?)
      bai_viet.dang_kich_hoat = false;
      bai_viet.save
      end
    end
    redirect_to :back
  end
  
  def khong_xoa
    nguoi_dung = nguoi_dung_hien_tai()
    if nguoi_dung.present? && nguoi_dung.is_admin && params[:uid].present?
      bai_viet = BaiViet.find(params[:uid])
      if (bai_viet.present?)
      bai_viet.dang_kich_hoat = true;
      bai_viet.save
      end
    end
    redirect_to :back
  end

  def chi_tiet_danh_muc
  end

  def danh_muc
  end

  def them_danh_muc
  end

  def tao_moi
    @danh_muc = DanhMuc.find_by_slug(params[:danh_muc])
    @danh_muc_ngon_ngu = @danh_muc.danh_muc_ngon_ngus.select{ |v| v.ngon_ngu == I18n.locale.to_s }
    if (@danh_muc_ngon_ngu.empty?)
      @danh_muc_ngon_ngu = @danh_muc.danh_muc_ngon_ngus[0]
    else
      @danh_muc_ngon_ngu= @danh_muc_ngon_ngu[0]
    end
    @nguoi_dung = nguoi_dung_hien_tai
    if (params[:commit])
      bai_viet = BaiViet.create({
        :tieu_de => params[:tieu_de],
        :noi_dung => params[:noi_dung],
        :slug => params[:slug],
        :danh_muc => @danh_muc,
        :nguoi_dung => @nguoi_dung,
        :ngon_ngu => params[:ngon_ngu]
      })
      if bai_viet.present?
        redirect_to url_for :action=>'cap_nhat', :id => bai_viet.id.to_s
      else
        flash.now[:loi] = t('kiem_tra_lai_thong_tin')
      end
    end
  end
end
